package cn.tedu.autoshow.commons.ex.handler;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult handlerServiceException(ServiceException e){
        log.info("出现ServiceException异常");
        log.info("异常信息：{}",e.getMessage());

        /*return new JsonResult().setState(ServiceCode.SERVICE_ERR).setMessage(e.getMessage());*/
        return JsonResult.fail(e.getServiceCode(), e.getMessage());
    }

    @ExceptionHandler
    public JsonResult handlerBindException(BindException e){
        log.info("出现ServiceException异常");
        log.info("异常信息：{}",e.getMessage());

        //随机报错
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST, e.getFieldError().getDefaultMessage());

        //整合为一个字符串报错
        //StringBuilder stringBuilder = new StringBuilder();
        /*StringJoiner stringJoiner = new StringJoiner(",","填写信息错误,","!");
        List<FieldError> fieldErrors = e.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            String defaultMessage = fieldError.getDefaultMessage();
            stringJoiner.add(defaultMessage);
        }
        return stringJoiner.toString();*/

        //整合为一个数组报错
        /*List<String> list = new ArrayList<>();
        List<FieldError> fieldErrors = e.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            list.add(fieldError.getDefaultMessage());
        }
        return list;*/
    }

    @ExceptionHandler
    public JsonResult handlerConstraintViolationException(ConstraintViolationException e){
        log.info("出现ConstraintViolationException异常");
        log.info("异常信息：{}",e.getMessage());
        String message = null;
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            message = constraintViolation.getMessage();
        }
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST, message);
    }

    @ExceptionHandler({
            InternalAuthenticationServiceException.class,
            BadCredentialsException.class
    })
    public JsonResult handlerAuthenticationException(AuthenticationException e){
        log.info("AuthenticationException");
        log.info("异常信息：{}",e.getMessage());
        String message = "登录失败，用户名或密码错误！";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED, message);
    }

    @ExceptionHandler
    public JsonResult handlerDisabledException(DisabledException e){
        log.info("DisabledException");
        log.info("异常信息：{}",e.getMessage());
        String message = "登录失败，账号已经被禁用！";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED_DISABLE, message);
    }

    @ExceptionHandler
    public JsonResult handlerAccessDeniedException(AccessDeniedException e){
        log.info("AccessDeniedException");
        log.info("异常信息：{}",e.getMessage());
        String message = "账号权限不足，禁止访问！";
        return JsonResult.fail(ServiceCode.ERR_FORBIDDEN, message);
    }

    @ExceptionHandler
    public JsonResult handlerThrowable(Throwable e){
        log.warn("出现Throwable异常");
        //log.warn("异常信息：{}",e.getMessage());
        log.warn("异常：",e); // 取代 e.printStackTrace();

        return JsonResult.fail(ServiceCode.ERR_UNKNOWN, "服务器忙，请稍后再试！");
    }

}
